package com.fadadianqi.commons;

import com.yeqiangwei.commons.util.ValidatorHelper;
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

import java.io.File;
import java.io.IOException;
import java.io.Writer;

public class VelocityWrapper {

    private static final Logger logger = Logger.getLogger(VelocityWrapper.class);
    static VelocityEngine VE;
    /*static String PROPS_PATH = null;
	static String VM_PREFIX_PATH = null;*/

    static {
		/*if(PROPS_PATH == null){
			PROPS_PATH = Constants.WEB_INF_PATH + "classes" + File.separator + "conf" + File.separator + "velocity.properties";
		}
		if(VM_PREFIX_PATH == null){
			VM_PREFIX_PATH = Constants.WEB_INF_PATH + "velocity" + File.separator;
		}*/
        if (VE == null) {
            VE = new VelocityEngine();
            logger.info("VelocityEngine initialized!!!");
        }
    }

    public static final VelocityEngine getVelocityInstance() {
        try {
            String baseDir = Constants.getBaseDir();
            String path = baseDir + "conf" + File.separator + "velocity.properties";
            File file = new File(path);
            logger.info(path);
            if (!file.exists()) {
                logger.error("Not found " + path);
            }
            VE.init(path);
        } catch (Exception e) {
            logger.error(e.toString());
            e.printStackTrace();
        }
        return VE;
    }

    public static final Template getTemplate(VelocityEngine ve, String name) {
        if (ve == null || ValidatorHelper.isEmpty(name)) {
            logger.error("ve == null || ValidatorHelper.isEmpty(name)");
            return null;
        }
        try {
            return ve.getTemplate(name);
        } catch (ResourceNotFoundException e) {
            logger.error(e.toString());
        } catch (ParseErrorException e) {
            logger.error(e.toString());
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return null;
    }

    public static final Template getTemplate(String name) {
        return VelocityWrapper.getTemplate(VelocityWrapper.getVelocityInstance(), name);
    }

    public static final void merge(Template template, Context context, Writer writer) {
        if (template == null) {
            logger.error("###### Template is null...");
            return;
        }
        try {
            template.merge(context, writer);
        } catch (ResourceNotFoundException e) {
            logger.error(e.toString());
        } catch (ParseErrorException e) {
            logger.error(e.toString());
        } catch (MethodInvocationException e) {
            logger.error(e.toString());
        } catch (IOException e) {
            logger.error(e.toString());
        }
    }
}
